<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content  :native-scrollbar="false" title="邀请人员" closable>
            <n-form :model="model" :rules="rules" ref="formRef">
                <n-form-item label="名称" path="name">
                    <n-input placeholder="请填写人员名称" v-model:value="model.name" maxlength="32" show-count clearable />
                </n-form-item>
                <n-form-item label="手机号" path="mob">
                    <n-input placeholder="请填写手机号" v-model:value="model.mob" maxlength="11" show-count clearable />
                </n-form-item>
                <n-form-item label="邀请码" path="pwd">
                    <n-input placeholder="请填写邀请码" v-model:value="model.pwd" maxlength="10" show-count clearable />
                </n-form-item>
                <n-form-item label="邀请码有效时长（小时）" path="hour">
                    <n-input-number v-model:value="model.hour" :min="1" :max="24 * 7" clearable />
                </n-form-item>
                <n-form-item label="是否发送邀请短信" path="sms">
                    <n-switch v-model:value="model.sms">
                        <template #checked>发送</template>
                        <template #unchecked>不发送</template>
                    </n-switch>
                </n-form-item>
            </n-form>
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" :disabled="loading" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                    <n-button :disabled="loading" @click="execReset">
                        <template #icon>
                            <n-icon :component="RestoreRound" />
                        </template>
                        重置
                    </n-button>
                    <n-button type="primary" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        发送邀请
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, onBeforeMount } from 'vue'
import { CloseRound, CheckRound, RestoreRound } from '@vicons/material'

import { Http } from '@/libs'
import { useStore } from '@/store'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.3, 640))

const { $notification: notification } = window

const loading = ref(false)

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        default: {},
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const formRef = ref<any>(null)

const model = ref<any>({})

const rules = {
    name: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写人员名称',
    },
    mob: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写手机号',
    },
    pwd: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写邀请码',
    },
    hour: {
        type: 'number',
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写邀请码有效期',
    },
}

const execReset = () => {
    model.value = {
        id: prop.item.id,
        name: '',
        mob: '',
        pwd: '',
        hour: 24,
        sms: false,
    }
}

const execSave = (e: Event) => {
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/biz/merchant/clerk/add', model.value).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功添加人员',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}

onBeforeMount(() => {
    execReset()
})
</script>
